export default async function (source, params, headers) {
  const query = new URLSearchParams(params)
  const response = await fetch(
    (headers.baseURL || '') + '/special-work/diaozhuangWorkTicket/getDiaoZhuangFormInfo?' + query,
    {
      method: 'get',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        ...(headers || {})
      }
    }
  )

  if (response.status !== 200 || !response.ok) {
    // 尝试读取错误信息
    const errorText = await response.text()
    console.error('API请求失败:', {
      status: response.status,
      statusText: response.statusText,
      url: response.url,
      responseText: errorText
    })
    return null
  }

  // 检查响应内容类型
  const contentType = response.headers.get('content-type')
  if (!contentType || !contentType.includes('application/json')) {
    const text = await response.text()
    console.error('响应不是JSON格式:', text)
    return null
  }

  try {
    const data = await response.json()
    if (data.success) {
      return fillData(source, data.data)
    } else {
      return null
    }
  } catch (error) {
    console.error('JSON解析失败:', error)
    return null
  }
}

function fillData(source, data) {
  if (data && Object.keys(data).length > 0) {
    source.header[0].value = data.licenseId
    source.data[0][0].value = data.applicantUnitName
    source.data[0][1].value = data.operationalUnitName
    source.data[0][2].value = data.workApproveTime
    source.data[1][0].value = data.siteDiaozhuangName
    source.data[1][1].value = data.workToolsNames
    source.data[1][2].value = data.diaozhuangContent
    source.data[2][0].value = data.diaozhuangPerson
    source.data[2][1].value = data.siSuoRen
    source.data[2][2].value = data.guardian
    source.data[3][0].value = data.commanderPerson
    source.data[3][1].value =
      data.diaozhuangWeight +
      '(t)、' +
      ['', '一级', '二级', '三级', '四级'][data.diaozhuangOriginalLevel / 1] // 吊物质量(t)及作业级别
    source.data[4][0].value = data.identConsequence?.join(',')

    if (data.diaozhuangEstStartTime && data.diaozhuangEstEndTime) {
      source.data[5][0].value = data.diaozhuangEstStartTime + '至' + data.diaozhuangEstEndTime
    } else if (!data.diaozhuangEstStartTime && data.diaozhuangEstEndTime) {
      source.data[5][0].value = data.diaozhuangEstEndTime + '止'
    } else if (data.diaozhuangEstStartTime && !data.diaozhuangEstEndTime) {
      source.data[5][0].value = data.diaozhuangEstStartTime + '起'
    } else {
      source.data[5][0].value = ''
    }

    source.data[6][0].data.list =
      data.workSafeMeasuresVOS?.map((pm, ind) => {
        return [
          ind + 1,
          pm.measures,
          ['否', '是'][pm.isInvole / 1],
          {
            text: pm.signPersonName,
            mode: 'img',
            modeUrl: pm.sign,
            modeStyle: {
              margin: 'auto'
            },
            modeConfig: {
              title: pm.signPersonName
            }
          }
        ]
      }) || []

    source.data[7][0].data[0][0].value = data.workSafeDisclosureVOS?.join('、')
    source.data[7][0].data[0][1].value = data.safeAcceptDisclosureVOS?.join('、')

    source.data[8][0].data =
      data.approveInfoVOS?.map((pm) => {
        return {
          label: pm.approveNode,
          value: pm.approveOpinion,
          sign: pm.approveSign,
          signMode: 'img',
          signTitle: pm.approvePersonName,
          time: pm.approveTime
        }
      }) || []
    if (data.workCheckAcceptRecordVO) {
      source.data[8][0].data.push({
        label: '完工验收',
        value: ['同意', '拒绝'][data.workCheckAcceptRecordVO.checkAcceptResult / 1],
        sign: data.workCheckAcceptRecordVO.sign,
        signMode: 'img',
        signTitle: data.workCheckAcceptRecordVO.checkAcceptPersonName,
        time: data.workCheckAcceptRecordVO.signTime
      })
    }
  }

  return source
}
